/*
 * @Description: 孵化 Store
 * @Author: xuyang
 * @Date: 2019-06-09 15:06:14 
 * @Last Modified by: XuYang
 * @Last Modified time: 2019-06-17 13:44:17
 */

import { observable, action, computed, runInAction } from 'mobx';
import {
    getHatchStatisticsDataApi,
    getHatchListDataApi,
    depositApi,
    applyUnlockApi,
    getProfitLogApi
} from '../api/hatch';

export default class Login {
    @observable hatchStatistics = {
        depositsum: 0,
        accumulated: 0,
        yesterday_income:0,
        title:'孵化记录',
        hatch: true,
        icon: 'lock',
        url:'HatchControl'
    }; //孵化统计 
    @observable hatchList = [];
    @observable currentPage = 1;
    @observable pageSize = 5;
    @observable total = 0;
    @observable lastPage = 0;

    @observable profitList = [];
    @observable profitCurrentPage = 1;
    @observable profitPageSize = 5;
    @observable profitTotal = 0;
    @observable profitLastPage = 0;
    constructor(rootStore) {
        this.rootStore = rootStore;
    }
    @action setHatchStatistics (hatch){
        this.hatchStatistics.depositsum =  hatch.depositsum;
        this.hatchStatistics.accumulated = hatch.accumulated;
        this.hatchStatistics.yesterday_income = hatch.yesterday_income;
    }
    @action getHatchStatistics (){
        return this.hatchStatistics;
    }
    @action setHatchList (hatchList){
        if(hatchList.current_page !== this.currentPage && hatchList.current_page !== 1){
            this.hatchList = [...this.hatchList,...hatchList.data]
        }else{
            this.hatchList = hatchList.data;
        }
        this.currentPage = parseInt(hatchList.current_page);
        this.pageSize = hatchList.per_page;
        this.total = hatchList.total;
        this.lastPage = hatchList.last_page;
    }
    @action setHatchListEmpty (){
        this.hatchList = [];
        this.currentPage = 1;
        this.pageSize = 5;
        this.total = 0;
        this.lastPage = 0;
    }
    @action getHatchList (){
        return this.hatchList;
    }
    @action setProfitLog(data) {
        if(data.current_page !== this.page && data.current_page !== 1){
            this.profitList = [...this.profitList,data.data]
          }else{
              this.profitList = data.data;
          }
    }
    @action setProfitPage(data){
        this.profitCurrentPage = parseInt(data.current_page);
        this.profitPageSize = data.per_page;
        this.profitTotal = data.total;
        this.profitLastPage = data.last_page;
    }
    /**
     * 获取充币地址
     */
    @action getHatchStatisticsData = async () => {
        try { 
            storage.load({ key: 'token' }).then(async (ret) => {
                if (ret) {
                    const response = await getHatchStatisticsDataApi(ret);
                    if(response.status === 11001){
                        //登录失效 重新登录
                        this.rootStore.loginStore.loginInvalid();
                        return;
                    } 
                    if (response.status === 10000) {
                        runInAction(() => {
                            this.setHatchStatistics(response.data);
                        });
                    } else if(response.status === 90001){
                        //数据为空
                    }else {
                        this.rootStore.uiStore.setErrorMsg(response.message);
                    }
                }
              }).catch(() => {
                this.rootStore.uiStore.setErrorMsg(response.message);
              });
           
        } catch(e) {
            this.rootStore.uiStore.setErrorMsg(response.message);
        }
    }
    /**
     * 获取充币地址
     */
    @action getHatchListData = async (params) => {
        try { 
            storage.load({ key: 'token' }).then(async (ret) => {
                if (ret) {
                    const response = await getHatchListDataApi(ret,params);
                    if (response.status === 10000) {
                        runInAction(() => {
                            this.setHatchList(response.data.list);
                        });
                    } else if(response.status === 90001){
                        //数据为空
                        this.setHatchListEmpty()
                    }else {
                        this.rootStore.uiStore.setErrorMsg(response.message);
                    }
                }
              }).catch(() => {
                this.rootStore.uiStore.setErrorMsg(response.message);
              });
           
        } catch(e) {
            this.rootStore.uiStore.setErrorMsg(response.message);
        }
    }
    @action deposit = async (params,cb) => {
        try { 
            storage.load({ key: 'token' }).then(async (ret) => {
                if (ret) {
                    const response = await depositApi(ret, params);
                    if (response.status === 10000) {
                        runInAction(() => {
                            cb && cb();
                        });
                    } else if(response.status === 90001){
                        //数据为空
                    }else {
                        this.rootStore.uiStore.setErrorMsg(response.message);
                    }
                }
              }).catch(() => {
                this.rootStore.uiStore.setErrorMsg(response.message);
              });
           
        } catch(e) {
            this.rootStore.uiStore.setErrorMsg(response.message);
        }
    }
    @action applyUnlock = async (params,cb) => {
        try { 
            storage.load({ key: 'token' }).then(async (ret) => {
                if (ret) {
                    const response = await applyUnlockApi(ret, params);
                    if (response.status === 10000) {
                        runInAction(() => {
                            cb && cb();
                        });
                    }else {
                        this.rootStore.uiStore.setErrorMsg(response.message);
                    }
                }
              }).catch(() => {
                this.rootStore.uiStore.setErrorMsg(response.message);
              });
           
        } catch(e) {
            this.rootStore.uiStore.setErrorMsg(response.message);
        }
    }
    /**
     * 收益记录
     */
    @action getProfitLog = async(params) => {
        try { 
            storage.load({ key: 'token' }).then(async (ret) => {
                if (ret) {
                    const response = await getProfitLogApi(ret, params);
                    if (response.status === 10000) {
                        runInAction(() => {
                            this.setProfitLog(response.data);
                            this.setProfitPage(response.data)
                        });
                    } else if(response.status === 90001){
                        //数据为空
                            this.setProfitLog([])
                    }else {
                        this.rootStore.uiStore.setErrorMsg(response.message);
                    }
                }
              }).catch(() => {
                this.rootStore.uiStore.setErrorMsg(response.message);
              });
           
        } catch(e) {
            this.rootStore.uiStore.setErrorMsg(response.message);
        }
    }
}